package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.commom.CustomException;
import com.example.dao.DishDao;
import com.example.dto.DishDto;
import com.example.entity.Dish;
import com.example.entity.DishFlavor;
import com.example.service.CategoryService;
import com.example.service.DishFlavorService;
import com.example.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {
    @Resource
    private DishFlavorService dishFlavorService;

    @Resource
    private DishService dishService;

    @Resource
    private CategoryService categoryService;

    @Override
    @Transactional
    public void saveWithFlover(HttpServletRequest request, DishDto dishDto) {
        this.save(dishDto);
        List<DishFlavor> flavors=dishDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            item.setCreateTime(LocalDateTime.now());
            item.setUpdateTime(LocalDateTime.now());
            Long id=(Long) request.getSession().getAttribute("employee");
            item.setCreateUser(id);
            item.setUpdateUser(id);
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public DishDto findId(Long id) {
        Dish dish=this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        if (list==null){
            return null;//当前菜品没有口味
        }
        //封装口味
        dishDto.setFlavors(list);
        return dishDto;//返回数据到主页
    }

    @Override
    @Transactional
    public void updateByDishId(DishDto dishDto) {
        //更新菜品表
        this.updateById(dishDto);
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());//判断
        dishFlavorService.remove(queryWrapper);
        //加载当前提交过来的口味数据
        List<DishFlavor> flavors=dishDto.getFlavors();
        flavors=flavors.stream().map((item)->{
            //进行插入操
            // ;
            //Long id=(Long)request.getSession().getAttribute("employee");
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);


    }

    @Override
    public void removeWithDish(List<Long> ids) {
        //先查看菜品状态是否可以被删除
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dish::getId,ids);
        queryWrapper.eq(Dish::getStatus,1);
        int count=this.count(queryWrapper);
        if (count>0){
            throw new CustomException("当前菜品正在售卖，无法进行删除");
        }
        //可以删除的话，先删除菜品所对应的口味表数据
        LambdaQueryWrapper<DishFlavor> dishFlavorQueryWrapper=new LambdaQueryWrapper<>();
        dishFlavorQueryWrapper.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(dishFlavorQueryWrapper);
        this.removeByIds(ids);
    }
}
